package com.smyh.UIM.host.param;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.Toast;

import com.smyh.UIM.CustomDialog;
import com.smyh.UIM.HostFragment;
import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.bean.Protocol101SetInfo;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.model.host.param.Param101Model;
import com.smyh.protcol.host.HostCMDUtil;

import java.util.HashMap;
import java.util.Map;

public class Param_101Activity extends Fragment {
    ScrollView ktableView;
    Button read_state_btn,modif_state_btn;
    View view;
    Dialog dialog;
    static boolean isRefresh = false;

   public static Map<String,Protocol101SetInfo> param101Map = new HashMap<>(); //Protocol101SetInfo info;

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        if(view == null){
            view = inflater.inflate(R.layout.activity_host_pram101, container, false);
        }
        initView();
        initEvent();
        return view;
    }

    private void initEvent() {
        //点击读取按钮
        read_state_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MinaClientThread.session != null){
                    if(!HostFragment.connection_btn.isChecked()){
                        Toast.makeText(getContext(), "请建立连接", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(!HostFragment.ready_btn.isChecked()){
                        Toast.makeText(getContext(), "请ready", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    isRefresh = false;
                    dialog = CustomDialog.createLoadingDialog(getContext(),"正在读取数据，请稍后...");
                    MinaClientThread.session.write(HostCMDUtil.readParam(1));
                    final Long time = System.currentTimeMillis();
                    dialog.show();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true){
                                if(System.currentTimeMillis() - time >5000||isRefresh){
                                    dialog.dismiss();
                                    if(!isRefresh){
                                        handler1.sendMessage(new Message());
                                    }
                                    return;
                                }
                                try {
                                    Thread.sleep(300);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }).start();
                }else{
                    Toast.makeText(getContext(), "没有连接WIFI", Toast.LENGTH_SHORT).show();
                }
            }
        });


        //点击修改按钮
        modif_state_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MinaClientThread.session != null){
                    //101参数修改
                    MinaClientThread.session.write(Param101Model.writeParam());
                    Toast.makeText(MainActivity.context, "101参数修改下发...", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getContext(), "没有连接WIFI", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(MainActivity.context, "101参数读取失败...", Toast.LENGTH_SHORT).show();
            isRefresh = false;
        }
    };

    static Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Param101Model.setTableLayout();
            Toast.makeText(MainActivity.context, "101参数读取成功...", Toast.LENGTH_SHORT).show();
            isRefresh = true;
        }
    };

    public static void refreshParam101(Protocol101SetInfo model){
        if(model != null){
            /**
             * MainCahce.chooseDeviceAddr射频物理地址
             */
            param101Map.put(MainCahce.chooseDeviceAddr,model);
        }
        handler.sendMessage(new Message());
    }
    private void initView() {
        ktableView = (ScrollView) view.findViewById(R.id.ktable);
        read_state_btn = (Button) view.findViewById(R.id.read_state);
        modif_state_btn = (Button) view.findViewById(R.id.modif_state);
        if(ktableView.getChildCount()== 0) {
            ScrollView parent = (ScrollView) Param101Model.getTableLayout(getContext()).getParent();
            if (parent != null&&parent.getChildCount()>0) {
                parent.removeAllViews();
            }
            ktableView.addView(Param101Model.getTableLayout(getContext()));
        }
    }
}
